﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Threading;
using ttg_internal.Utilities.Common.Other;
using ttg_internal.Utilities.ParameterManager.Other;
using ttg_internal.Utilities.ParameterManager.View;
using ttg_internal.utils;

namespace ttg_internal.Utilities.ParameterManager.ViewModel
{
    /// <summary>
    /// Composition wrapper around <see cref="ParameterSet" />
    /// </summary>
    internal class CompositeParameterSet
    {
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        public CompositeParameterSet(Dispatcher dispatcher, ParameterSet parameterSet)
        {
            _dispatcher = dispatcher;
            _parameterSet = parameterSet;
        }

        // Private fields
        private readonly Dispatcher _dispatcher;
        private readonly ParameterSet _parameterSet;
        private CompositeCollection _compositeParameters;

        /// <summary>
        /// Collection composited of SubSets and Parameters of underlying <see cref="ParameterSet" />
        /// </summary>
        public CompositeCollection CompositeParameters
        {
            get
            {
                return _compositeParameters
                       ?? (_compositeParameters = new CompositeCollection
                                                      {
                                                          new CollectionContainer { Collection = new ConverterObservableCollection<CompositeParameterSet>(_dispatcher, _parameterSet.SubSets, set => new CompositeParameterSet(_dispatcher, (ParameterSet)set)) },
                                                          new CollectionContainer { Collection = new ConverterObservableCollection<Parameter>(_dispatcher, _parameterSet.Parameters, param => (Parameter)param) },
                                                      });
            }
        }

        public string Name
        {
            get { return _parameterSet.Name; }
        }
    }

    /// <summary>
    /// Model for <see cref="MainView" />
    /// </summary>
    internal class MainViewModel : BindableObject
    {
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        public MainViewModel(Dispatcher dispatcher, GlobalInfo globalInfo)
        {
            ParameterRoot = new ObservableCollection<CompositeParameterSet> { new CompositeParameterSet(dispatcher, globalInfo.Parameters.Root) };
        }

        /// <summary>
        /// Root of parameter tree
        /// </summary>
        public IList<CompositeParameterSet> ParameterRoot { get; private set; }
    }
}
